<?php

namespace common\models\datas;

use common\components\behaviors\UpdateCacheBehavior;
use Yii;
use yii\helpers\ArrayHelper;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use common\components\extenders\MenuManage;
use doumiaoduo\ueditor\ImageUploadMultipleTrait2;

/**
 * This is the model class for table "{{%site}}".
 *
 * @property integer $id
 * @property string $name
 * @property string $domain
 * @property string $address
 * @property string $contact
 * @property string $header
 * @property integer $created_at
 * @property integer $updated_at
 */
class Site extends \common\components\bases\BaseModel
{
//    use ImageUploadMultipleTrait2;

    public $picture;



    public static function tableName()
    {
        return '{{%site}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['web_site_id', 'name', 'domain', 'address', 'contact', 'header', 'site_no','mode','is_buy_repeat'], 'required'],
            [['domain', 'site_no'], 'unique'],
            [['created_at', 'updated_at','is_buy_repeat','mode'], 'integer'],
            [['name', 'contact'], 'string', 'max' => 100],
            [['domain'], 'string', 'max' => 20],
            [['address'], 'string', 'max' => 200],
            [['header'], 'string', 'max' => 50],
            [['mode'], 'string', 'max' => 10],
            [['background_img'],'string','max'=>200],
            [['logo', 'background_img'], 'string']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('common', '自动编号'),
            'name' => Yii::t('common', '分店名称'),
            'domain' => Yii::t('common', '二级域名'),
            'site_no' => Yii::t('common', '分店代号'),
            'address' => Yii::t('common', '分店地址'),
            'contact' => Yii::t('common', '联系方式'),
            'header' => Yii::t('common', '负责人'),
            'created_at' => Yii::t('common', '创建时间'),
            'updated_at' => Yii::t('common', '修改时间'),
            'web_site_id' => Yii::t('common', '城市ID'),
            'logo' => Yii::t('common', '分店LOGO'),
            'background_img' => Yii::t('common', '分店背景图'),
            'mode' => Yii::t('common', '模式'),
            'is_buy_repeat' => Yii::t('common', '是否可预约同类商品'),
        ];
    }

    public function extraFields()
    {
        return [
            'backend_operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();
            },
        ];
    }


//转换时间格式
    public function fields()
    {
        $fields = parent::fields();
        $fields['updated_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->updated_at);
        };
        $fields['is_buy_repeat'] = function () {
           switch($this->is_buy_repeat){
               case 0:
                   return Yii::t('common', '否');
                   break;
               case 1:
                   return Yii::t('common', '是');
                   break;
           }
        };
        $fields['mode'] = function (){
            switch ($this->mode){
                case 1:
                    return \Yii::t('common','共同体');
                default:
                    return \Yii::t('common','综合体');
            }
        };
        return $fields;
    }


//修改时间
    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
            [
                'class' => UpdateCacheBehavior::className(),
                'cacheName' => 'site'
            ]
        ]);
    }
}
